
var TaskTree = new Object;

TaskTree.createModel = function(tasks) {
	var model = new Object();
	TaskTree.configureModel(model, tasks)
	return model;
}

TaskTree.configureModel = function(model, tasks) {
	
	model.getNumberOfNodes = function(path) {
		var length = 0;
		if(path==null) {
			length = tasks.getTasks().length;
		} else {
			var task = TaskTree.searchTask(tasks, path);
			if(task!=null) {
				length = task.getTasks().length;
			}
		}
		return length;
	}

	model.getNodeAtPath = function(path) {
		var nodeModel = null;
		var task = TaskTree.searchTask(tasks, path);
		if(task!=null) {
			var label = WindLabel.createView();
			label.setText(task.getTitle());
			nodeModel = new WindTreeNodeModel();
			nodeModel.setView(label);
		}
		return nodeModel;
	}
	
	model.onContextMenu = function(e, treeView, nodeView) {
		e.stopPropagation();
		var task = null;
		if(nodeView!=null) {
			var path = nodeView.getPath();
			task = TaskTree.searchTask(tasks, path);
		}
		TaskTree.showContextMenu(e, treeView, model, task);
		return false;
	}
}

TaskTree.updateModel = function(model, tasks) {
	TaskTree.configureModel(model, tasks)
}

TaskTree.createView = function(treeModel) {
	if(treeModel==null) {
		treeModel = TaskTree.createModel();
	}
	var treeView = WindTree.createView(treeModel);
	return treeView;
}

TaskTree.searchTask = function(tasks, path) {
	var taskDTO = null;
	
	if(path!=null) {
		var i = 0;
		var j = path[0];
		while(i<tasks.getTasks().length && i<=j) {
			if(i==j) {
				taskDTO = tasks.getTasks()[i];
			}
			i++;
		}
		
		if(path.length>1) {
			var tempPath = new Array();
			for(i=1; i<path.length; i++) {
				tempPath[tempPath.length] = path[i];
			}
			taskDTO = TaskTree.searchTask(taskDTO, tempPath);
		}
	}
	
	return taskDTO;
}

TaskTree.updateView = function(view, model) {
	WindTree.updateView(view, model);
}

TaskTree.showContextMenu = function(e, view, model, task) {

	var menuView = view.getContextMenu();

	if(menuView!=null) {
		WindWindow.removeView(menuView);
	}

	var menuModel = WindMenu.createModel();
	menuModel.getChildren = function(path) {
		var children = 0;
		if(path == null) {
			if(task==null) {
				children = 1;
			} else {
				children = 3;
			}
		}
		return children;
	}
	menuModel.getItem = function(path) {
		var item = null;
		if(path.length == 1) {
			var index = path[0];
			if(task==null) {
				if(index == 0) {
					item = new WindMenuItemModel();
					item.setText("New");
					item.setAction(function() {
						var dialog = TaskDialog.createView("insert");
						dialog.onClose = function(code) {
							if(code=="inserted") {
								tasks = WindData.execute("getTasks");
								TaskTree.updateModel(taskTreeModel, tasks);
								TaskTree.updateView(view, taskTreeModel);
								//CalendarWeekGrid.updateView(calendarView, tasks);
							}
						}
						dialog.open();
					});
				}
			} else {
				if(index == 0) {
					item = new WindMenuItemModel();
					item.setText("New");
					item.setAction(function() {
						var dialog = TaskDialog.createView("insert");
						dialog.onClose = function(code) {
							if(code=="inserted") {
								tasks = WindData.execute("getTasks");
								taskTreeModel = TaskTree.createModel(tasks);
								TaskTree.updateView(view, taskTreeModel);
								//CalendarWeekGrid.updateView(calendarView, tasks);
							}
						}
						dialog.open();
					});
				} else if(index == 1) {
					item = new WindMenuItemModel();
					item.setText("Edit");
					item.setAction(function() {
						var dialog = TaskDialog.createView("update", task);
						dialog.onClose = function(code) {
							if(code=="updated") {
								tasks = WindData.execute("getTasks");
								taskTreeModel = TaskTree.createModel(tasks);
								TaskTree.updateView(view, taskTreeModel);
								//CalendarWeekGrid.updateView(calendarView, tasks);
							}
						}
						dialog.open();
					});
				} else if (index == 2) {
					item = new WindMenuItemModel();
					item.setText("Delete");
				}
			}
		}
		return item;
	}
	
	menuView = WindMenu.createContextMenuView(menuModel, e);
	
	view.setContextMenu(menuView);
	
	WindWindow.addView(menuView);

}